﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCSlideShowList : System.Web.UI.UserControl
    {
        ISlideShowService _slideShowService;
        private ILocationService _locationService;
        private ILanguageService _languageService;

        protected void Page_Load(object sender, EventArgs e)
        {
            _locationService = new LocationService();
            _languageService = new LanguageService();
            if (!IsPostBack)
            {
                Initialize();
            }
            cboLocation.SelectedIndexChanged += new EventHandler(cboLocation_SelectedIndexChanged);
            cboLanguage.SelectedIndexChanged += new EventHandler(cboLanguage_SelectedIndexChanged);
        }

        void cboLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            _slideShowService = new SlideShowService();
            List<RedHouse.Data.SlideShow> list = _slideShowService.GetAllSlideShows();
            rptNews.DataSource = list.Where(s => s.LocationId == int.Parse(cboLocation.SelectedValue) && s.LanguageId.ToString() == cboLanguage.SelectedValue).ToList();
            rptNews.DataBind();
        }

        void cboLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            _slideShowService = new SlideShowService();
            List<RedHouse.Data.SlideShow> list = _slideShowService.GetAllSlideShows();
            rptNews.DataSource = list.Where(s => s.LocationId == int.Parse(cboLocation.SelectedValue) && s.LanguageId.ToString() == cboLanguage.SelectedValue).ToList();
            rptNews.DataBind();
        }

        private void Initialize()
        {
            //Databinding
            cboLocation.DataSource = _locationService.GetAllLocation();
            cboLocation.DataTextField = "Name";
            cboLocation.DataValueField = "ID";
            cboLocation.DataBind();

            cboLanguage.DataSource = _languageService.GetAllLanguage();
            cboLanguage.DataTextField = "Description";
            cboLanguage.DataValueField = "ID";
            cboLanguage.DataBind();

            _slideShowService = new SlideShowService();
            List<RedHouse.Data.SlideShow> list = _slideShowService.GetAllSlideShows();
            rptNews.DataSource = list.Where(s => s.LocationId == int.Parse(cboLocation.SelectedValue) && s.LanguageId.ToString() == cboLanguage.SelectedValue).ToList();
            rptNews.DataBind();
        }
    }
}